package com.ruoyi.web.controller.common;

import com.qiniu.util.Auth;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ImageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.Mac;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "图片上传七牛云")
@RestController
@RequestMapping("/qiniuOSS")
public class QiniuOSSController extends BaseController {

    @Autowired
    private ImageUtils imageUtils;
    /**
     * 上传一张图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件",notes = "上传文件")
    @PostMapping("upload")// 接口名称和描述
    public AjaxResult image(@RequestParam(value = "file", required = false) MultipartFile file
    ) {
        if (file==null) {
            return  AjaxResult.error("上传图片为空！");
        }
        Map<String,String> url =  imageUtils.uploadImageQiniu(file);
        return AjaxResult.success(url);
    }

    @ApiOperation(value = "上传文件",notes = "支持批量上传文件")
    @PostMapping("uploadFiles")
    public AjaxResult image(@RequestParam(value = "files", required = false) MultipartFile[] files) {
        if (files == null || files.length == 0) {
            return AjaxResult.error("上传文件不能为空！");
        }

        List<Map<String,String>> resultList = new ArrayList<>();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                continue; // 跳过空文件
            }
            Map<String,String> url = imageUtils.uploadImageQiniu(file);
            resultList.add(url);
        }

        return AjaxResult.success(resultList);
    }




    @RequestMapping(value = "/imageMp4",method = RequestMethod.POST)
    @ApiOperation(value = "上传视频",notes = "上传视频")    // 接口名称和描述
    public AjaxResult imageMp4(@RequestParam(value = "file", required = false) MultipartFile file
    ) {
        if (file==null) {
            return AjaxResult.error("上传视频为空！");
        }
        Map<String,String>  url =  imageUtils.uploadImageQiniuMP4(file);
        return success(url);
    }




}
